package ysu.sxm.ptjc.Controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.web.bind.annotation.*;
import ysu.sxm.ptjc.Domin.RecordList;
import ysu.sxm.ptjc.Service.DefectQueryService;
import ysu.sxm.ptjc.Service.DefectRecordService;
import ysu.sxm.ptjc.Service.ProduceInfoService;
import ysu.sxm.ptjc.pojo.DefectQueryInfo;
import ysu.sxm.ptjc.pojo.DefectRecordInfo;
import ysu.sxm.ptjc.pojo.ProduceInfo;
import ysu.sxm.ptjc.pojo.SignInfo;

import java.util.List;

@RestController
@RequestMapping(path = "/record",produces = "application/json; charset=utf-8")
public class DefectRecordServlet {
    @Autowired
    ProduceInfoService produceInfoService;
    @Autowired
    DefectQueryService defectQueryService;
    @Autowired
    DefectRecordService recordService;

    @RequestMapping(path = "/check",method = RequestMethod.POST)
    public String RecordCheck(@RequestBody DefectRecordInfo defectRecordInfo) throws JSONException {
        System.out.print(defectRecordInfo);
        JSONObject returnjson = new JSONObject();
        if(defectRecordInfo.getLineId()==null){
            returnjson.put("code", 4);
        }
        else{
            returnjson.put("code",1);
        }
        ProduceInfo produceInfo;
        if(defectRecordInfo.getLineId()==null){
            produceInfo=produceInfoService.findOneByProduceId(Integer.parseInt(defectRecordInfo.getProduceId()));
            returnjson.put("line_id",produceInfo.getLineId());
        }else {
            produceInfo = produceInfoService.findOneByProduceIdAndLineid(Integer.parseInt(defectRecordInfo.getProduceId()), defectRecordInfo.getLineId().intValue());
        }
        DefectQueryInfo defectQueryInfo=defectQueryService.findOneByDefectNum(defectRecordInfo.getDefectNum());
        if(produceInfo!=null){
            if(defectRecordInfo.getLineId()==null){
                defectRecordInfo.setLineId(produceInfo.getLineId());
            }
            if(defectQueryInfo!=null){
                if(recordService.findOneByProduceIdAndLineId(defectRecordInfo.getProduceId(),defectRecordInfo.getLineId().intValue())){
                    returnjson.put("result",200);
                    returnjson.put("msg", "检查通过");
                    //returnjson.put("code", 1);
                    returnjson.put("producenam",produceInfo.getProduceName());
                    returnjson.put("defectQuery",defectQueryInfo.getDefectNam());
                }
                else{
                    returnjson.put("result",500);
                    returnjson.put("msg", "已有缺陷记录");
                    //returnjson.put("code", 1);
                }
            }
            else{
                returnjson.put("result",404);
                returnjson.put("msg", "未找到该缺陷");
                //returnjson.put("code", 1);
            }
        }
        else {
            returnjson.put("result",404);
            returnjson.put("msg", "未找到该产品");
            //returnjson.put("code", 1);
        }

        return returnjson.toJSONString();
    }

    @RequestMapping(path = "/add",method = RequestMethod.POST)
    public String RecordAdd(@RequestBody DefectRecordInfo defectRecordInfo) throws JSONException {
        JSONObject returnjson = new JSONObject();
        if(recordService.addRecord(defectRecordInfo)){
            returnjson.put("result",200);
            returnjson.put("msg", "添加成功");
            returnjson.put("code", 2);
        }
        else{
            returnjson.put("result",500);
            returnjson.put("msg", "已有缺陷记录");
            returnjson.put("code", 2);
        }
        return returnjson.toJSONString();
    }

    @RequestMapping(path = "/show",method = RequestMethod.POST)
    public String RecordList(@RequestBody SignInfo signInfo) throws JSONException {
        JSONObject returnjson = new JSONObject();
        List<RecordList> list=recordService.AddRecordList(signInfo);
        JSONArray array=JSONArray.parseArray(JSON.toJSONString(list));
        returnjson.put("result",200);
        returnjson.put("msg", "查询成功");
        returnjson.put("list",array);
        returnjson.put("num",list.size());
        returnjson.put("code", 3);
        System.out.print("发送数据："+returnjson.toString());
        return returnjson.toJSONString();
    }

    @RequestMapping(path = "/change",method = RequestMethod.POST)
    public String RecordChange(@RequestBody DefectRecordInfo defectRecordInfo)throws JSONException{
        JSONObject returnjson = new JSONObject();
        if(recordService.update(defectRecordInfo)){
            returnjson.put("result",200);
            returnjson.put("msg", "修改成功");
            returnjson.put("code", 6);
        }
        else{
            returnjson.put("result",500);
            returnjson.put("msg", "修改失败");
            returnjson.put("code", 6);
        }
        System.out.print("发送数据："+returnjson.toString());
        return returnjson.toJSONString();
    }
}